package org.subside.Play.ReadLocalFile;

import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * TXT文件
 */
@Slf4j
public class TxtUtil {

    /**
     * 获取文本内容
     * @return 文件内容
     */
    public static String readTxtFile(String filePath) {
        String txtStr = "";
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                log.error("可读文件不存在[{}]", file.getAbsolutePath());
            }
            txtStr = FileUtils.readFileToString(file, CharsetUtil.UTF_8);
        } catch (IOException e) {
            log.error("readTxtFile [{}] is error", filePath);
        }
        txtStr = txtStr.replace("\n", "");
        txtStr = txtStr.replace("\\\\r", "");
        txtStr = txtStr.replace("\\\\t", "");
        return txtStr;
    }


    /**
     * .
     * 写入数据
     *
     * @param outPath 输出路径
     * @param context 内容
     */
    public static void writeFile(String outPath, String context) {
        try {
            File file = new File(outPath);
            FileUtils.write(file, context, CharsetUtil.UTF_8, false);
        } catch (IOException e) {
            log.error("writeFile is error {}", e);
        }
    }
}
